﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;

namespace Vit.Report.XmlWebReport
{
    public class TableCellElement : CellElement
    {
        private TableRowElement row;

        public TableCellElement(TableRowElement row, string topLeftPosition, string bottomRightPosition, string content)
            : base(row.Table.Page, topLeftPosition, bottomRightPosition, content)
        {
            this.row = row;
            this.row.Cells.Add(this);
            CellIndex = this.row.Cells.Count;
            Index = CellIndex;
        }

        public TableCellElement(TableRowElement row, RectanglePosition rect, string content)
            : base(row.Table.Page, rect, content)
        {
            this.row = row;
            this.row.Cells.Add(this);
            CellIndex = this.row.Cells.Count;
            Index = CellIndex;
        }

        protected override void Initialize()
        {
            base.Initialize();
            PositionType = PositionType.Fix;
            this.CssClass += " table-cell";
            YOffset = 0;
        }

        public override void CalculatePosition()
        {
            if (string.IsNullOrEmpty(ID))
                ID = string.Format("{0}_{1}_{2}", page.ID, RectanglePosition.TopLeftPosition, RectanglePosition.BottomRightPosition);

            if (IsPrintable == false)
                return;

            //if (this.IsHtml)
            //{
            this.content = ReportHelper.TrimHtml(this.content);
            //}

            if (this.IsHeader || this.IsFooter)
                return;
            
            if (PositionType == PositionType.Auto)
            {
                for (int i = 0; i < Index; i++)
                {
                    var positionType = page.Elements[i].PositionType;
                    if (positionType == PositionType.RuntimeCalculationSource || positionType == PositionType.RuntimeCalculation)
                    {
                        PositionType = PositionType.RuntimeCalculation;
                        break;
                    }
                }
            }

            if (PositionType == PositionType.RuntimeCalculation)
            {
                RectanglePosition.Move(page.XOffset, page.YOffset);
            }

            string split = string.Empty;
            int yOffset = ReportHelper.AppendWithContent(this.Font, page, this.RectanglePosition, this.PositionType, ref this.content, out split);
            if (yOffset > 0)
                this.YOffset += yOffset;

            RectanglePosition rightRect = null;
            RectanglePosition bottomRect = null;
            this.RectanglePosition.Crop(page.ContentRectanglePosition, out rightRect, out bottomRect);

            if (rightRect != null)
            {
                page.EnsureExtraXPage();
                CreateExtraElement(page.ExtraXPage, rightRect, "");
            }

            if (bottomRect != null && !string.IsNullOrEmpty(split))
            {
                row.EnsureExtraYRow();
                TableCellElement cell = new TableCellElement(row.ExtraYRow, bottomRect, split);
                cell.ID = this.ID + "_extraYCell";
                cell.CellIndex = this.CellIndex;
            }
        }

        protected override void AddToPage()
        {
        }

        public int CellIndex { get; set; }
        public TableRowElement Row { get { return row; } }
        public int YOffset { get; set; }
    }
}
